<template>
  <el-form ref="ConfigForm" :model="formData" :rules="formRules" label-width="90px">
    <el-form-item v-if="action === 'edit'" prop="id">
      <input type="hidden" :model="formData.id">
    </el-form-item>
    <el-form-item label="所属系统" prop="sysId">
      <SystemType v-model="formData.sysId" />
    </el-form-item>
    <el-form-item label="配置名称" prop="keyName">
      <el-input v-model="formData.keyName" type="text" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="配置值" prop="keyValue">
      <el-input v-model="formData.keyValue" type="text" placeholder="请输入值" />
    </el-form-item>
    <el-form-item label="描述" prop="keyDesc">
      <el-input v-model="formData.keyDesc" type="textarea" :autosize="{ minRows: 3}" placeholder="请输入配置简介" :maxlength="255" />
    </el-form-item>
  </el-form>
</template>
<script>
import { get, save, update } from '@/api/admin/config';
import SystemType from '@/components/admin/system-type';

export default {
  name: 'ConfigFormDialog',
  components: {
    SystemType
  },
  props: {
    action: {
      type: String,
      default: 'add'
    }
  },
  data() {
    // 校验ConfigCode
    // const checkConfig = (rule, value, callback) => {
    //   if (value === '') {
    //     callback('配置名称不能为空！');
    //   } else {
    //     if (this.action === 'edit') {
    //       callback();
    //     }
    //     verifyConfig({ sysId: this.formData.sysId, keyName: value }).then(response => {
    //       debugger;
    //       if (response.data.code === 0) {
    //         callback();
    //       } else {
    //         callback(new Error('配置名称已存在!请替换'));
    //       }
    //     });
    //   }
    // };
    return {
      formRules: {
        keyName: [
          { required: true, message: '配置名称不能为空！', trigger: 'blur' }
        ],
        keyValue: [
          { required: true, message: '配置值不能为空！', trigger: 'blur' }
        ]
      },
      formData: {
        id: null,
        keyName: null,
        keyValue: null,
        keyDesc: null
      }
    };
  },
  methods: {
    getInfo(id) {
      get({ id: id }).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.formData.id = result.data.id;
          this.formData.sysId = result.data.sysId;
          this.formData.keyName = result.data.keyName;
          this.formData.keyValue = result.data.keyValue;
          this.formData.keyDesc = result.data.keyDesc;
        } else {
          this.$notify.error({
            title: result.code,
            message: result.msg
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.ConfigForm.validate(valid => {
        if (valid) {
          if (this.action === 'add') {
            const params = { ...this.formData };
            save(params).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.action === 'edit') {
            update(this.formData).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult(obj) {
      if (obj.code === 0) {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'success'
        });

        this.$parent.$parent.customFormInitOption.showModal = false;
        this.$parent.$parent.$refs.customTable.query();
        this.reset();
      } else {
        this.$notify.error({
          title: obj.code,
          message: obj.msg
        });
      }
    },
    reset() {
      this.formData.sonFormData = [];
      this.count = 0;
      this.$refs.ConfigForm.resetFields();
    }
  }
};
</script>
